package com.py.credit.service.impl;

import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.dao.CreditAppDao;
import com.py.credit.service.CreditAppService;
import com.py.credit.service.RemoteBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CreditAppServiceImpl implements CreditAppService {

    @Autowired
    private CreditAppDao creditAppDao;

    @Autowired
    private RemoteBaseService remoteBaseService;

    @Override
    public List<Map> collectionByDeptTop5() {
        // TODO Auto-generated method stub
        return creditAppDao.collectionByDeptTop5();
    }

    @Override
    public List<Map> dataQualityTop10() {
        // TODO Auto-generated method stub
        return creditAppDao.dataQualityTop10();
    }

    @Override
    public List<Map> mapAnalyse(String qybm) {
        // TODO Auto-generated method stub
        return creditAppDao.mapAnalyse(qybm);
    }

    @Override
    public List<Map> collectionByAreaTop5() {
        // TODO Auto-generated method stub
        return creditAppDao.collectionByAreaTop5();
    }

    @Override
    public List<Map> checkAnalyse() {
        // TODO Auto-generated method stub
        return creditAppDao.checkAnalyse();
    }

    @Override
    public List<Map> selectTop5Radar(String qybm) {
        // TODO Auto-generated method stub
        return creditAppDao.selectTop5Radar(qybm);
    }

    @Override
    public Map applicationAndCreditRepairCountByMonth() {
        Map<String, Object> map = new HashMap<>(8);
        map.putAll(creditAppDao.selectTop5Month().get(0));
        map.putAll(creditAppDao.selectApplicationByMonth().get(0));
        map.putAll(creditAppDao.selectCreditRepairByMonth().get(0));
        return map;
    }

    @Override
    @Transactional
    public void initDeptScore() {

        creditAppDao.delDeptScore();

        creditAppDao.initDeptScore();

        creditAppDao.updateReportCountScore();

        creditAppDao.updatePassrateScore();

        creditAppDao.updateTimelinessScore();

        creditAppDao.updateConstructionofcreditScore();

        /* creditAppDao.updateCreditappScore(); */
        RestServiceResult<List<Map>> data = remoteBaseService.getLoginTimesByDept();
        List<Map> listLoginData = null;
        if (data != null) {
            listLoginData = data.getData();
        }
        if (listLoginData != null && listLoginData.size() > 0) {
            for (int i = 0; i < listLoginData.size(); i++) {
                int loginTime = (Integer) listLoginData.get(i).get("countLogin");
                int deptId = (Integer) listLoginData.get(i).get("deptId");
                long shareTime = creditAppDao.selectShareLogCountByDept(deptId);
                creditAppDao.updateCreditappOfLoginScore(deptId, loginTime + shareTime);
            }
        }

        creditAppDao.updateTotalScore();

    }

}
